﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using webnew.Common;
using WebPageBase;
namespace webnew.Systems
{
    public partial class S_compList : ListBase        //注：此页面继承了ListBase页，相关的操作都是ListBase页中执行了
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            _EditSrc = "S_compEdit.aspx";            //编辑页路径
            _ListTbName = "sys_company";             //表名
            _Grdview = grdView;                      //主列表控件
            _DelType = DeleteType.FalseDel;          //逻辑删除  
            btnExcelVisble = false; //设置导出不可见                     
            if (!IsPostBack)
            {
                SetTitle(true, "后台管理 >> 单位管理");
                //设置当前公司类型为空选项
                webnew.Common.CommonHelper.DataDdlBind(ddlCompLevel, new BLL.Sys_SystemType().GetconList("系统单位级别"), "TypeName", "TypeName");
                ddlCompLevel.Items.Insert(0, new ListItem("--请选择--", Guid.Empty.ToString()));
                AspNetPager1_PageChanged(null, null);
            }

        }

        /// <summary>
        /// 检索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1_PageChanged(null, null);             //刷新页面
        }

        /// <summary>
        /// 重写删除事件,使删除后刷新页面;
        /// </summary>
        protected override void _Del()
        {
            //根据选中的公司ID,判断是否能删除
            if (new BLL.Sys_Company().isCanDel(GetSelecetedIDs(grdView)) == false)
            {
                ShowMsg ="单位使用中,不能删除!";
                return;
            }
            //----------系统日志model
          
            Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
            modelLog.UserID = UserInfor.Id;                  //用户ID
            modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
            modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
            modelLog.CreateDate = DateTime.Now;           //创建时间

            string error = "";                   //获取错误信息

            try
            {
                base._Del();
            }
            catch (Exception e)
            {
                error = e.ToString();
            }
            finally
            {
                //添加一条系统日志
                new BLL.Sys_Log().AddSysLog(error, "删除", modelLog, "系统单位", "单位，单位名称为" + GetNames("compname", _ListTbName));
            }

            AspNetPager1_PageChanged(null, null);
        }
       

        /// <summary>
        /// 分页控件事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>    
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //分页的查询表名
            System.Text.StringBuilder strtable = new System.Text.StringBuilder();
            //要查询的字段
            string strFeild = "a.*,(select compname from Sys_Company where id= a.ParentID) as ParentIDName";
            strtable.Append(" Sys_Company a ");
            //if (ddlCompLevel.SelectedValue != "一级单位")
            //{
            //    strtable.Append(" join  Sys_Company b on  a.ParentID=b.id ");
            //    strFeild = "a.*,b.compname as ParentIDName";
            //}
            System.Text.StringBuilder _conditon = new System.Text.StringBuilder();  //分页的查询的条件                  
            _conditon.Append(" a.isdel <>1 and  a.compname like '%" + txtCompName.Text.Trim().Replace("'", "") + "%'");
            _conditon.Append(" and  a.ERPCode like '%" + txtERPCode.Text.Trim().Replace("'", "") + "%' ");
            if (ddlCompLevel.SelectedValue != Guid.Empty.ToString())
            {
                _conditon.Append(" and a.CompanyLevel='" + ddlCompLevel.SelectedValue + "'");
               // if (ddlCompLevel.SelectedValue != "一级单位") _conditon.Append(" and b.compname like '%" + txtParentIDName.Text + "%'");
            }
            AspNetPager1.RecordCount = new BLL.Sys_Company().GetCount(strtable.ToString(),_conditon.ToString());             //根据条件获取相关的行数
            webnew.Common.CommonHelper.DataGrdBind(grdView, new BLL.Sys_Company().GetPageList(strFeild, _conditon.ToString(), grdView.PageSize, AspNetPager1.CurrentPageIndex, strtable.ToString(), "a.CompanyLevel,a.sort,a.compname,a.id", true));
        }


        /// <summary>
        /// 显示条数下拉框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grdView.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            AspNetPager1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            AspNetPager1_PageChanged(null, null);
        }


        protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //鼠标经过变色
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "GridViewMouseover(this)");
                e.Row.Attributes.Add("onmouseout", "GridViewMouseout(this)");
                e.Row.Attributes.Add("onclick", "GridViewonclick(this)");
                //if (e.Row.Cells[1].Text == "1") e.Row.Cells[1].Text = "一级单位";
                //else if (e.Row.Cells[1].Text == "2") e.Row.Cells[1].Text = "二级单位";
               // e.Row.Cells[6].Text = e.Row.Cells[6].Text == Guid.Empty.ToString() ? "" : e.Row.Cells[6].Text;
                e.Row.Cells[8].ToolTip = e.Row.Cells[8].Text == "&nbsp;" ? "" : e.Row.Cells[8].Text;
                e.Row.Cells[9].Text = e.Row.Cells[9].Text == "0" ? "启用" : "停用";
                
            }
        }

    


        ///// <summary>
        ///// 重写导出事件;
        ///// </summary>
        //protected override void _Excel()
        //{
        //    System.Text.StringBuilder _conditon = new System.Text.StringBuilder();  //分页的查询的条件 
        //    _conditon.Append(" isdel<>1 and  comptype like '%" + UcCompType1.GetSelectedValue() + "%'");
        //    _conditon.Append(" and  compname like '%" + txtCompName.Text.Trim().Replace("'", "") + "%'");
        //    _conditon.Append(" order by sort,comptype,id desc");
        //    webnew.Common.CommonHelper.DataGrdBind(grdToExcel, new BLL.Sys_Company().GetList(_conditon.ToString()));
        //    ToExecl.GridviewToExecl(grdToExcel, "单位管理", Convert.ToUInt16(grdToExcel.Columns.Count));
        //}

       //protected void grdToExcel_DataBound(object sender, EventArgs e)
        //{
        //    for (int i = 0; i < grdToExcel.Rows.Count;i++)
        //    {
        //        if (grdToExcel.Rows[i].Cells[4].Text == "0")
        //        {
        //            grdToExcel.Rows[i].Cells[4].Text = "启用";
        //        }
        //        else
        //            grdToExcel.Rows[i].Cells[4].Text = "停用";
        //    }
        //}
     
    }
}
